package java学习.leetcode.editor.cn.数组;

import java.util.*;

/**
 * @author 刘世锦
 * 2022-09-28 14:21:16	 当前时间
 */
//给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素。 
//
// 
//
// 示例 1： 
//
// 
//输入：matrix = [[1,2,3],
// 				[4,5,6],
// 				[7,8,9]]
//输出：[1,2,3,6,9,8,7,4,5]
// 
//
// 示例 2： 
//
// 
//输入：matrix = [[1,2,3,4],
// 				 [5,6,7,8],
//				 [9,10,11,12]]
//输出：[1,2,3,4,8,12,11,10,9,5,6,7]
// 
//
// 
//
// 提示： 
//
// 
// m == matrix.length 
// n == matrix[i].length 
// 1 <= m, n <= 10 
// -100 <= matrix[i][j] <= 100 
// 
// Related Topics 数组 矩阵 模拟 
// 👍 1222 👎 0

public class 螺旋矩阵{
	public static void main(String[] args) {
		Solution solution = new 螺旋矩阵().new Solution();
//		int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
//输入：matrix = [[1,2,3,4],
// 				 [5,6,7,8],
//				 [9,10,11,12]]
//输出：[1,2,3,4,8,12,11,10,9,5,6,7]
		int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
		int[] matrix123 = new int[] {1,2,3,4};
//				{8,4},{0,-1}};
		solution.spiralOrder(matrix);


	}

	public static void BiShi_main12(String[] args) {

		String str = "AAB CCD EDC";
		StringBuilder res = new StringBuilder();
		String[] s = str.split(" ");
		for (int i = 0; i <s.length ; i++) {
			StringBuffer stb = new StringBuffer(s[i]);
			s[i] = stb.reverse().toString();
			res.append(s[i]).append(" ");
		}
		System.out.println(res);
//		System.out.println(Arrays.toString(s));

		Integer[] a = new Integer[] { 6, 3, 9, 3, 2, 4, 5, 7 };
		Integer[] b = new Integer[] { 5, 8, 6, 2, 1, 9 ,7};
		List<Integer> list1 = Arrays.asList(a);
		List<Integer> list2 = Arrays.asList(b);
		Collections.sort(list1);
		Collections.sort(list2);

		Collection realA = new ArrayList<Integer>(list1);
		Collection realB = new ArrayList<Integer>(list2);
		realA.retainAll(realB);
		System.out.println("交集结果：" + realA);
		char c = 'A';
		int num = 5;
		switch (c){
			case 'B': num++;
			case 'A':num++;
			case 'Y':num++;
				break;
			default:
				num--;
		}
		System.out.println(num);
	}

	public static void BiShi_main3(String[] args) {
		Scanner cin=new Scanner(System.in);
		double x,y;
		x=y=0;

		System.out.println("请输入月利润（万）：");
		x=cin.nextInt();
		if(x<=10) {
			y=x*0.1;
		}else if(x>10&&x<20) {
			y=10*0.1+(x-10)*0.075;
		}else if(x>=20&&x<40) {
			y=10*0.1+10*0.075+(x-20)*0.05;
		}else if(x>=40&&x<60) {
			y=10*0.1+10*0.075+20*0.05+(x-40)*0.03;
		}else if(x>=60&&x<100) {
			y=10*0.1+10*0.075+20*0.05+20*0.03+(x-60)*0.015;
		}else if(x>=100) {
			y=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(x-100)*0.01;
		}
		System.out.println("奖金是"+y+"万");
		cin.close();
	}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public List<Integer> spiralOrder1(int[][] matrix) {
		 List<Integer> list = new ArrayList<>();
		 int row = matrix.length-1  ;
		 int column = matrix[0].length-1;
		 int top = 0,bottom = row ,left = 0,right = column;
		while (left <= right && top <= bottom) {
			 for (int i = left; i <=right ; i++) {
				 list.add(matrix[top][i]);
			 }
			 for (int i = top+1; i <=bottom ; i++) {
				 list.add(matrix[i][right]);
			 }
			if (left<right&&top<bottom){
				for (int i = right-1; i >left ; i--) {
					list.add(matrix[bottom][i]);
				}
				for (int i = bottom;i>top;i--){
					list.add(matrix[i][left]);
				}
			}
			top++;
			right--;
			bottom--;
			left++;
		}
		System.out.println(list);
		 return list;
	}
	public List<Integer> spiralOrder(int[][] matrix) {
		List<Integer> list = new ArrayList<>();
		int row = matrix.length-1  ;
		int column = matrix[0].length-1;
		int top = 0,bottom = row ,left = 0,right = column;
		while (left <= right && top <= bottom) {
			for (int i = left; i <=right ; i++) {
				list.add(matrix[top][i]);
			}
			top++;
			for (int i = top; i <=bottom ; i++) {
				list.add(matrix[i][right]);
			}
			right--;
			/*
			 * 临界值判断
			 *       0 1
			 *  0 [[2,5],
			 *  1  [8,4],
			 *  2  [0,-1]]
			 * 遍历到-1时：此时 left==right 需要 bottom->top ↑ 遍历
			 *  top 0->1     left  0
			 *  bottom 2  right 1->0
			 */
			if (left<=right&&top<=bottom){ // 保证当中间只剩一行/一列时，继续按照 ↑ / ←
				for (int i = right; i >=left ; i--) {
					list.add(matrix[bottom][i]);
				}
				bottom--;
				for (int i = bottom;i>=top;i--){
					list.add(matrix[i][left]);
				}
				left++;
			}
		}
		return list;
	}

}
//leetcode submit region end(Prohibit modification and deletion)

}
